<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include '../Module/newsModule.php';
include '../Module/CategoryModule.php';
include '../Module/AlbumModule.php';
$categoryid=$_REQUEST["cateid"];
$caModule=new CategoryModule();
$alModule=new AlbumModule();
$dtca=$caModule->getCategorys();

$newModule=new newsModule();
if(isset($_REQUEST["id"])){
    $id= $_REQUEST["id"];
    $item=$newModule->getNew($id);
    genErorMSG($newModule->geterror());
}
?>

    
    <div class="mybutton icon tooltip" title="Trở Về" 
         onclick="loadhtml('./Controller/?AUnit=<?=(isset($_REQUEST["cateid"])?"04_about&cateid=$categoryid":"07_system")?>',$('#box-content'));">
        <div><img src="./images/icon/left1.png"/><span>Trở Về</span></div></div>
    <div class="mybutton icon spaceright tooltip" title="Tải Lại" 
         onclick="<?=isset($id)?"loadhtml('./Controller/?AUnit=05_detail".(isset($_REQUEST["cateid"])?"&cateid=$categoryid":"")."&id=".$id."',$('#box-content'));":"loadhtml('./Controller/?AUnit=05_detail&cateid=$categoryid',$('#box-content'));"?>">
        <div><img src="./images/icon/arrow_refresh1.png"/><span>Tải Lại</span></div></div>
    <div class="mybutton icon tooltip<?=isset($item["delete"])?" disable":""?>" 
         title="Lưu"
         onclick="<?=isset($item["delete"])?"":(isset($id)?"update();":"save();")?>"
         ><div><img src="./images/icon/media_floppy_green.png"/><span>Lưu</span></div></div>
    <div class="mybutton icon tooltip<?=isset($id)?"":" disable"?>" 
         onclick="<?=isset($id)?"loadhtml('./Controller/?AUnit=05_detail&cateid=$categoryid',$('#box-content'));":""?>"
         title="Thêm Mới"
         ><div><img src="./images/icon/addg.png"/><span>Thêm Mới</span></div></div>

    <div style="clear: both"></div>
    <div style="height: 40px;width: 100%;text-align: center">
        <h1><?=isset($id)?"Chỉnh Sửa : ":"Thêm mới"?><?=$item["title"]?></h1>
    </div>
    <div style="width: 100%">
        <table style="width:100%;" class="_form">
                    <tr>
                        <td style="">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txttitlei" placeholder="" value="<?=$item["title"]?>"/>
                                    <input type="hidden" id="txtidi" placeholder="" value="<?=$item["id"]?>"/>
                                    <div class="label"><label>Tiêu đề</label></div>
                                </div>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td>
                            <div class="_input-label" type="select">
                            <div class="input" type="select">
                                <div class="select">
                                    <select id="cbxcategoryi">
                                        <?
                                        if(!isset($item["category"]))$item["category"]=$_SESSION["category"];
                                        foreach ($dtca as $row){
                                            if($row["id"]==$item["category"])
                                                echo "<option selected=1 value='".$row["id"]."'>".$sp.$row["name"]."</option>";
                                            else
                                                echo "<option value='".$row["id"]."'>".$sp.$row["name"]."</option>";


                                        }
                                        unset($row);unset($dtca);unset($caModule);
                                        ?>
                                    </select>
                                </div>
                                <div class="label"><label>Danh mục</label></div>
                            </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtsecond_title" placeholder="" value="<?=$item["title_second"]?>"/>
                                    <div class="label"><label>Tiêu đề phụ</label></div>
                                </div>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtsmall_image" placeholder="" value="<?=$item["small_image"]?>"/>
                                    <div class="label"><label>Hình nhỏ</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txtsmall_image' );"
                                title="Chọn hình ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    
                                    <input type="text" id="txtimagei" name="txtimagei" placeholder="" value="<?=$item["image"]?>"/>
                                    <div class="label"><label>Hình Ảnh</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right: 8px;cursor: pointer"
                                 onclick="BrowseServer( 'Images:/', 'txtimagei' );"
                                 title="Chọn hình ảnh"
                                 >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td style="position: relative">
                            <div class="_input-label hidden">
                                <div class="input">
                                    <input type="text" id="txtimg1" placeholder="" value="<?=$item["img1"]?>"/>
                                    <div class="label"><label>Hình 1</label></div>
                                </div>
                                <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                    onclick="BrowseServer( 'Images:/', 'txtimg1' );"
                                    title="Chọn hình ảnh"
                                    >
                                    <img src="./images/icon/16/folder_explore.png"/>
                                </div>
                            </div>
                            <div class="_input-label" type="select">
                            <div class="input" type="select">
                                <div class="select">
                                    <select id="cbxalbumidi">
                                        <option value="0">--- Chọn Album ---</option>
                                        <?
                                        $albumid=$item["album"];
                                        $albums=$alModule->getAlbums();
                                        foreach ($albums as $row){
                                            if($row["id"]==$albumid)
                                                echo "<option selected=1 value='".$row["id"]."'>".$row["album"]."</option>";
                                            else
                                                echo "<option value='".$row["id"]."'>".$row["album"]."</option>";


                                        }
                                        unset($row);unset($albums);unset($alModule);
                                        ?>
                                    </select>
                                </div>
                                <div class="label"><label>Album</label></div>
                            </div>
                            </div>
                        </td>
                    </tr>
                    <tr style="display: none">
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtimg2" placeholder="" value="<?=$item["img2"]?>"/>
                                    <div class="label"><label>Hình 2</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txtimg2' );"
                                title="Chọn hình ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtimg3" placeholder="" value="<?=$item["img3"]?>"/>
                                    <div class="label"><label>Hình 3</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txtimg3' );"
                                title="Chọn hình ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                    </tr>
                    <tr style="display: none">
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtimg4" placeholder="" value="<?=$item["img4"]?>"/>
                                    <div class="label"><label>Hình 4</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txtimg4' );"
                                title="Chọn hình ảnhe"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                        <td class="space"></td>
                        <td style="position: relative">
                            <div class="_input-label">
                                <div class="input">
                                    <input type="text" id="txtimg5" placeholder="" value="<?=$item["img5"]?>"/>
                                    <div class="label"><label>Hình 5</label></div>
                                </div>
                            </div>
                            <div class="icon16 hover50 tooltip" style="position: absolute;top:12px;right:8px;cursor: pointer"
                                onclick="BrowseServer( 'Images:/', 'txtimg5' );"
                                title="Chọn hình ảnh"
                                >
                                <img src="./images/icon/16/folder_explore.png"/>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <div style="width:200px;display: inline">
                            <input type="checkbox" <?=($item["album"])?" checked=1":""?> id="ckalbum"/>Add images to new Photo Gallery
                            </div>
                        </td>
                        
                    </tr>
                    <tr>
                        <td colspan="3">
                            <div class="input-full">
                                <div class="label"><label>Nội dung ngắn gọn</label></div>
                            </div>
                            <div style="padding:27px 0px 0 0;" id="editor_brief">
                                
                            </div>
                            <textarea class="_s _w100 mceEditor" id="txtbrief" rows="3" cols="103" 
                                            style="width: 100%;height: 100px"

                                            ><?=$item["brief"]?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">

                            
                            <div class="input-full">
                                <div class="label"><label>Nội dung</label></div>
                            </div>
                            <div style="padding:27px 0px 0 0;" id="editor_content">

                            </div>
                                <textarea class="_s _w100 mceEditor" id="txtdes" rows="3" cols="103" 
                                            style="width: 100%;height: 400px"

                                            ><?=$item["content"]?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" style="">
                            <span style="color: blue">Thêm lúc : <?=$item["insert"]?></span>
                            <?
                            $u=$item["update"];$d=$item["delete"];
                            if(isset($item["update"])){
                                echo "<br/><span style='color: orange'>cập nhật lúc : $u</span>";
                            }
                            if(isset($item["delete"])){
                                echo "<br/><span style='color: red'>Xóa lúc : $d</span>";
                            }
                            ?>

                        </td>
                    </tr>
                </table>


        <span id="warningmsg"></span>
    </div>
    <div style="clear: both"></div>

    <div class="mybutton icon tooltip" title="Trở Về" 
         onclick="loadhtml('./Controller/?AUnit=<?=(isset($_REQUEST["cateid"])?"04_about&cateid=$categoryid":"07_system")?>',$('#box-content'));">
        <div><img src="./images/icon/left1.png"/><span>Trở Về</span></div></div>
    <div class="mybutton icon spaceright tooltip" title="Tải Lại" 
         onclick="<?=isset($id)?"loadhtml('./Controller/?AUnit=05_detail".(isset($_REQUEST["cateid"])?"&cateid=$categoryid":"")."&id=".$id."',$('#box-content'));":"loadhtml('./Controller/?AUnit=05_detail&cateid=$categoryid',$('#box-content'));"?>">
        <div><img src="./images/icon/arrow_refresh1.png"/><span>Tải Lại</span></div></div>
    <div class="mybutton icon tooltip<?=isset($item["delete"])?" disable":""?>" 
         title="Lưu"
         onclick="<?=isset($item["delete"])?"":(isset($id)?"update();":"save();")?>"
         ><div><img src="./images/icon/media_floppy_green.png"/><span>Lưu</span></div></div>
    <div class="mybutton icon tooltip<?=isset($id)?"":" disable"?>" 
         onclick="<?=isset($id)?"loadhtml('./Controller/?AUnit=05_detail&cateid=$categoryid',$('#box-content'));":""?>"
         title="Thêm Mới"
         ><div><img src="./images/icon/addg.png"/><span>Thêm Mới</span></div></div>

    <div style="clear: both"></div>
    
    <script>
        $(document).ready(function(){
            $(".tooltip").tipTip({defaultPosition:"top", edgeOffset: 10});
//            if($('.html-wysiwyg').length)$('.html-wysiwyg').wysiwyg();
//            $('.wysiwyg').css("width","100%");
//            $('.wysiwyg iframe').css("width","100%");
//            $("#txttitlei").focus();
//            var config={
//                
//                contentsCss:"css/style.css"
//            };
//            editor=CKEDITOR.appendTo( 'editor_content', config, $("#txtdes").val() );
//            CKFinder.setupCKEditor( editor, 'ckfinder' ) ;
//            
//            editor2=CKEDITOR.appendTo( 'editor_brief', config, $("#txtbrief").val() );
//            CKFinder.setupCKEditor( editor2, 'ckfinder' ) ;
            tinyMCE.init({
                    mode : "textareas",
                    theme : "advanced",
                    content_css : "/Coffee/css/style.css",
                    editor_selector : "mceEditor",
                    editor_deselector : "mceNoEditor",
                    theme_advanced_resizing : true,
                    theme_advanced_resize_horizontal : false
            });
        });
        
        function save(){
            process("add");
        }
        function update(){
            process("edit");
        }
        function process(action){
            showmsg("Đợi tý. Đang xử lý.",null);
            if(!_FcheckFilled($("#txttitlei").val())){$("#txttitlei").focus();return;}
            if(!_FcheckFilled(tinyMCE.get('txtbrief').getContent())){$("#txtbrief").focus();return;}
            if(!_FcheckFilled(tinyMCE.get('txtdes').getContent())){$("#txtdes").focus();return;}
            var surl="./Controller/";
            
            var sdata={
                    "controller":"NewController",
                    "action":action,
                    "id":$("#txtidi").val(),
                    "category":$("#cbxcategoryi").val(),
                    "image":$("#txtimagei").val(),
                    "img1":$("#txtimg1").val(),
                    "img2":$("#txtimg2").val(),
                    "img3":$("#txtimg3").val(),
                    "img4":$("#txtimg4").val(),
                    "img5":$("#txtimg5").val(),
                    "title":$("#txttitlei").val(),
                    "brief":tinyMCE.get('txtbrief').getContent(),
                    "des":tinyMCE.get('txtdes').getContent(),
                    "second_title":$("#txtsecond_title").val(),
                    "small_image":$("#txtsmall_image").val(),
                    "album":$('#cbxalbumidi').val()
                    
            };
            jQuery.ajax({
                    type:"POST", 
                    data:sdata, 
                    dataType:"text", 
                    url:surl, 
                    success: function (data){
                        if(data=="true"){
                            $("#warningmsg").html("Thành Công.Tải lại trang trong 2 giây.");
                            $("#warningmsg").show();
                            setTimeout(function() {
                                $("#warningmsg").hide();
                                hidemsg();
                            },3000);
                            
                        }else{
                            hidemsg();
                            showerrorbox(null,data);
                            $("#warningmsg").html(data);
                            $("#warningmsg").show();
                        }

                    },
                    error: function (xhr, ajaxOptions, thrownError){
                            $("#warningmsg").html("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+thrownError);
                            $("#warningmsg").show();
                    }
            });

        }
    </script>
